﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LingYue_PictureProcessCenter.Panel;
using LingYue_PictureProcessCenter.Core.HardwareInfo;
using LingYue_PictureProcessCenter.Core.PictureInfo;
using LingYue_PictureProcessCenter.Core.ProcessInfo;
using Microsoft.Win32;
using System.Diagnostics;

namespace LingYue_PictureProcessCenter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BasePictureInfoImpl BasePictureInfoImpl { set; get; }
        private HardwareInfo HardwareInfo { set; get; }

        public MainWindow()
        {
            BasePictureInfoImpl = new BasePictureInfoImpl();
            HardwareInfo = new HardwareInfo();
            InitializeComponent();
        }

        private void UseRealESR_Click(object sender, RoutedEventArgs e)
        {
            if (ConfigPanel.Children.Count != 0 && ConfigPanel.Children[0] is IConfigPanel)
            {
                if (((IConfigPanel)ConfigPanel.Children[0]).IsRunning)
                {
                    MessageBox.Show("当前任务尚未完成");
                    return;
                }
            }
            ConfigPanel.Children.Clear();
            ConfigPanel.Children.Add(new RealESRGAN(BasePictureInfoImpl, HardwareInfo));
        }

        private void SelectInputPathButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog Dialog = new OpenFileDialog();
            Dialog.Multiselect = false;
            Dialog.Title = "输入图片";
            Dialog.Filter = "图片文件(*.jpg,*.png,*.wepb)|*.jpg;*.png;*.wepb";
            if (Dialog.ShowDialog() == true)
            {
                InputPathTextBox.Text = Dialog.FileName;
                OutputPathTextBox.Text = System.IO.Directory.GetParent(Dialog.FileName)!.FullName;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            Process? RunningProc = null;
            if (ConfigPanel.Children[0] is RealESRGAN)
            {
                RunningProc = (ConfigPanel.Children[0] as RealESRGAN)!.CmdProcess;
            }
            RunningProc?.Kill();
            base.OnClosed(e);
        }
    }
}